import request from '../libs/request';
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        request
          .post(url, params,{
              headers: {
                  'Content-Type': 'application/json;charset=UTF-8'  //请求头
              }
          })
          .then(response => {
              resolve(response.data)
          })
          .catch(error => {
              reject(error)
          })
    })
}

export function get(url, params ={}) {
    return new Promise((resolve, reject) => {
        request
          .get(url,{
              params: params
          },{
              headers: {
                  'Content-Type': 'application/json;charset=UTF-8'  //请求头
              }
          })
          .then(response => {
              resolve(response.data)
          })
          .catch(error => {
              reject(error)
          })
    })
}

export function uploadPost (url, formData) {
    if (formData instanceof FormData) {
        return new Promise((resolve, reject) => {
            request.request({
                  url:  url,
                  method: "post",
                  data: formData,
                  headers: {
                      'Content-Type': 'multipart/form-data'  //请求头
                  }
              })
              .then(response => {
                  resolve(response.data);
              })
              .catch(error => {
                  reject(error);
              })
        })
    } else {
        console.error('格式错误，需要传入FormData格式');
    }
}


export function formatParams (params) {
    let formData = new FormData();
    formData.append("params", JSON.stringify(params));
    return formData;
}
